home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 3D / SetupGL / SetupGL Main.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  19.9 KB  |  699 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SetupGL Main.c
  3.  
  4.     Contains:    An example of the use of the SeupGL utility code.  This sample builds a context and lets the user control
  5.                 the specifications on the fly, rebuilding the context when the request changes.  Controls are:
  6.                 
  7.                 Left/right arrows: smaller or larger resolution context
  8.                 
  9.                 Up/down arrows: 32/16 bit pixel depth toggle
  10.                 
  11.                 left/right bracket: chnage requested screen down or up one
  12.                 
  13.                 w: go to windowed mode
  14.                 
  15.                 f: go to fullscreen mode            
  16.  
  17.     Written by:    Geoff Stahl
  18.  
  19.     Copyright:    1999 Apple Computer, Inc., All Rights Reserved
  20.  
  21.     Change History (most recent first):
  22.  
  23.  
  24.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  25.                 ("Apple") in consideration of your agreement to the following terms, and your
  26.                 use, installation, modification or redistribution of this Apple software
  27.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  28.                 please do not use, install, modify or redistribute this Apple software.
  29.  
  30.                 In consideration of your agreement to abide by the following terms, and subject
  31.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  32.                 copyrights in this original Apple software (the "Apple Software"), to use,
  33.                 reproduce, modify and redistribute the Apple Software, with or without
  34.                 modifications, in source and/or binary forms; provided that if you redistribute
  35.                 the Apple Software in its entirety and without modifications, you must retain
  36.                 this notice and the following text and disclaimers in all such redistributions of
  37.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  38.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  39.                 Apple Software without specific prior written permission from Apple.  Except as
  40.                 expressly stated in this notice, no other rights or licenses, express or implied,
  41.                 are granted by Apple herein, including but not limited to any patent rights that
  42.                 may be infringed by your derivative works or by other works in which the Apple
  43.                 Software may be incorporated.
  44.  
  45.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  46.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  47.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  49.                 COMBINATION WITH YOUR PRODUCTS.
  50.  
  51.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  52.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  53.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  54.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  55.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  56.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  57.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58.     
  59.          <7>     3/21/00    ggs     Removed grey region call from drag, not needed
  60.          <6>     1/26/00    ggs     Add preflight support, handle failure conditions throughout app
  61.                                     better
  62.          <5>     1/24/00    ggs     fixed Apple menu selection, fixed aglfont texture row length
  63.                                     problem, fixed suspend/resume, fixed update
  64.          <4>    12/18/99    ggs     Fix headers
  65.          <3>    12/18/99    ggs     Added window/fullscreen toggle support and error window
  66.          <2>    11/28/99    ggs     Standard toolbox application
  67.          <1>    11/28/99    ggs     Initial Add
  68.          <6>     9/14/99    GGS        Corrected buffer rect handling and cleaned up code
  69.          <5>     7/14/99    GGS        Fixed multi-monitor window centering
  70.          <4>     7/13/99    GGS        Add work around for over zealous checking in single buffer DSp context attributes
  71.          <3>     7/5/99     GGS        Now correctly handle multi-monitor (DSp front buffer for single device; Window on top of context for multiple devices)
  72.          <2>     5/28/99    GGS     Added better multi-monitor support, clean code, corrected blanking bug, added timing, correct pixel formats
  73.          <1>        ?        ?      Initial build
  74. */
  75.  
  76.  
  77. // system includes ----------------------------------------------------------
  78.  
  79. #include <Devices.h>
  80. #include <Dialogs.h>
  81. #include <DriverServices.h>
  82. #include <Events.h>
  83. #include <LowMem.h>
  84. #include <TextEdit.h>
  85. #include <ToolUtils.h>
  86. #include <Windows.h>
  87.  
  88. #include <math.h>
  89. #include <stdio.h>
  90. #include <string.h>
  91.  
  92. #include <gl.h>
  93.  
  94. // project includes ---------------------------------------------------------
  95.  
  96. #include "SetupGL.h"
  97.  
  98. // prototypes ---------------------------------------------------------------
  99.  
  100. void InitToolbox(void);
  101. Boolean SetUp (void);
  102. void DoMenu (SInt32 menuResult);
  103. void DoKey (SInt8 theKey, SInt8 theCode);
  104. void DoUpdate (void);
  105. void DoEvent (void);
  106. void CleanUp (void);
  107.  
  108. void drawGL(AGLContext aglContext, pstructGLInfo pglInfo);
  109.  
  110. void DrawFrameRate (GLuint fontList);
  111. void DrawContextInfo (GLuint fontList);
  112.  
  113. void DrawPSringGL (Str255 pstrOut, GLuint fontList);
  114. void DrawCSringGL (char * cstrOut, GLuint fontList);
  115. GLuint BuildFontGL (AGLContext ctx, GLint fontID, Style face, GLint size);
  116. void DeleteFontGL (GLuint fontList);
  117.  
  118.  
  119. // statics/globals (internal only) ------------------------------------------
  120.  
  121. // Menu defs
  122. enum 
  123. {
  124.     kNumRes = 10,
  125.     kNumFreqs = 11,
  126.  
  127.     kMenuApple = 128,
  128.     kMenuFile = 129,
  129.     
  130.     kAppleAbout = 1,
  131.     kFileQuit = 1
  132. };
  133.  
  134. enum 
  135. {
  136.     kForegroundSleep = 0,
  137.     kBackgroundSleep = 32000
  138. };
  139.  
  140. const RGBColor    rgbWhite    = { 0xFFFF, 0xFFFF, 0xFFFF };
  141.  
  142. SInt32 gSleepTime = kForegroundSleep;
  143. Boolean gDone = false, gfFrontProcess = true;
  144.  
  145. Point gapntResolution [kNumRes] = {{300, 400}, {384, 512}, {480, 640}, {600, 800}, {624, 832}, {768, 1024}, {870, 1152}, {1024, 1280}, {1200, 1600}, {1440, 1920}};
  146. short gResCurrent = 2;
  147. long gaFreq [kNumFreqs] = {56, 60, 65, 67, 72, 75, 76, 85, 90, 100, 120};
  148. short gFreqCurrent = 7;
  149. short gNumDevice = 0;
  150. char gstrMode [256] = "";
  151.  
  152. structGLInfo glInfo;
  153. AGLDrawable aglDraw = 0;
  154. AGLContext aglContext = 0;
  155. DSpContextReference dspContext = 0;
  156. GLuint gFontList = 0;
  157.  
  158. WindowPtr gpWindowError = NULL;
  159.  
  160.  
  161. // functions (internal/private) ---------------------------------------------
  162.  
  163. void InitToolbox(void)
  164. {
  165.     MenuHandle menu;
  166.  
  167.     MaxApplZone ();
  168.  
  169.     InitGraf((Ptr) &qd.thePort);
  170.     InitFonts();
  171.     InitWindows();
  172.     InitMenus();
  173.     TEInit();
  174.     InitDialogs(nil);
  175.     InitCursor();
  176.     
  177.     qd.randSeed =  TickCount();
  178.  
  179.     // Init Menus
  180.     menu = NewMenu (kMenuApple, "\p\024");            // new  apple menu
  181.     InsertMenu (menu, 0);                            // add menu to end
  182.     AppendResMenu(menu, 'DRVR');
  183.     
  184.     menu = NewMenu (kMenuFile, "\pFile");            // new menu
  185.     InsertMenu (menu, 0);                            // add menu to end
  186.     AppendMenu (menu, "\pQuit/Q");                     // add items
  187.     
  188.     DrawMenuBar();
  189.  
  190. // --------------------------------------------------------------------------
  191.  
  192. Boolean SetUp (void)
  193. {
  194.     short i;
  195.     short fNum;
  196.  
  197.     InitToolbox ();
  198.     
  199.     // we want a context and a drawable    
  200.     gNumDevice = 0; // main device (1 = next device, etc.)
  201.     memset(&glInfo, 0, sizeof glInfo);
  202.     glInfo.width = gapntResolution [gResCurrent].h; // width of drawable (screen width in full screen mode)
  203.     glInfo.height = gapntResolution [gResCurrent].v; // height of drawable (screen height in full screen mode)
  204.     glInfo.fSizeMust = false;            // dspContext must be requested height (ignored in window mode)
  205.                                         // Note: This basically determines whether or not displays will step down to allocate or fail
  206.     glInfo.pixelDepth = 16;                // requested pixel depth
  207.     glInfo.fDepthMust = false;            // pixel depth must be set (if false then curretn depth will be used if able)
  208.     glInfo.fFullscreen = true;            // use DSp to get fullscreen?
  209.     glInfo.fAcceleratedMust = true;     // must renderer be accelerated?
  210.     glInfo.VRAM = 0 * 1048576;            // minimum VRAM (if not zero this is always required)
  211.     glInfo.textureRAM = 0 * 1048576;    // minimum texture RAM (if not zero this is always required)
  212.     glInfo.freq = gaFreq [gFreqCurrent]; // desired vertical refresh frquency in Hz (0 = any)
  213.     glInfo.fmt = 0;                        // output pixel format
  214.     
  215.     i = 0;
  216.     glInfo.aglAttributes [i++] = AGL_RGBA;
  217.     glInfo.aglAttributes [i++] = AGL_DOUBLEBUFFER;
  218.     glInfo.aglAttributes [i++] = AGL_NO_RECOVERY;
  219.     glInfo.aglAttributes [i++] = AGL_ACCELERATED;
  220.     glInfo.aglAttributes [i++] = AGL_DEPTH_SIZE;
  221.     glInfo.aglAttributes [i++] = 16;
  222.     glInfo.aglAttributes [i++] = AGL_ALL_RENDERERS;
  223.     glInfo.aglAttributes [i++] = AGL_NONE;
  224.     
  225.     if (PreflightGL (glInfo.fFullscreen))
  226.     {
  227.         BuildGL (&aglDraw, &aglContext, &dspContext, &gNumDevice, &glInfo);
  228.         if (!aglDraw || !aglContext)
  229.             DestroyGL (&aglDraw, &aglContext, &dspContext, &glInfo);
  230.     }
  231.         
  232.     if (!aglDraw || !aglContext)
  233.     {
  234.         Rect rectWin = {100, 100, 200, 400};
  235.         gpWindowError = (WindowPtr) NewCWindow (NULL, &rectWin, "\p", 0, kWindowFloatProc, (WindowPtr)-1, 0, 0);
  236.         ShowWindow ((GrafPtr)gpWindowError);
  237.     }
  238.     else
  239.     {    
  240.         GetFNum("\pMonaco", &fNum);                                    // build font
  241.         gFontList = BuildFontGL (aglContext, fNum, normal, 9);
  242.         // set mode string
  243.         sprintf (gstrMode, "%d: %d x %d x %d (%d Hz)", gNumDevice, glInfo.width, glInfo.height, glInfo.pixelDepth, glInfo.freq);
  244.     }
  245.     return true;
  246. }
  247.  
  248. // --------------------------------------------------------------------------
  249.  
  250. void DoMenu (SInt32 menuResult)
  251. {
  252.     SInt16 theMenu;
  253.     SInt16 theItem;
  254.     Str255 daName;
  255.     MenuRef theMenuHandle;
  256.         
  257.     theMenu = HiWord(menuResult);
  258.     theItem = LoWord(menuResult);
  259.     theMenuHandle = GetMenuHandle(theMenu);
  260.  
  261.     switch (theMenu)
  262.     {
  263.         case kMenuApple:
  264.             switch (theItem)
  265.             {
  266.                 case kAppleAbout:
  267.                     break;
  268.                 default:
  269.                     GetMenuItemText (theMenuHandle, theItem, daName);
  270.                     OpenDeskAcc(daName);
  271.                     break;
  272.             }
  273.             break;
  274.         case kMenuFile:
  275.             switch (theItem)
  276.             {
  277.                 case kFileQuit:
  278.                     gDone = true;
  279.                     break;
  280.             }
  281.             break;
  282.     }
  283.     HiliteMenu(0);
  284.     DrawMenuBar();
  285. }
  286.  
  287. // --------------------------------------------------------------------------
  288.  
  289. void DoKey (SInt8 theKey, SInt8 theCode)
  290. {
  291.     #pragma unused (theCode)
  292.     // up and down arrows change bit depth (stop at top and bottom)
  293.     // right and left arrows change size (stop at top and bottom)
  294.     // +(=) and - change frequency request
  295.     // [ and ] cnage requested display device
  296.     
  297.     short fNum;
  298.     
  299.     // resolution
  300.     if (theKey == '\35')
  301.     {
  302.         
  303.         if (++gResCurrent == kNumRes)
  304.             gResCurrent = 0;
  305.         glInfo.width = gapntResolution [gResCurrent].h;
  306.         glInfo.height = gapntResolution [gResCurrent].v;
  307.     }
  308.     else if (theKey == '\34')
  309.     {
  310.         if (--gResCurrent < 0)
  311.             gResCurrent = (short) (kNumRes - 1);
  312.         glInfo.width = gapntResolution [gResCurrent].h;
  313.         glInfo.height = gapntResolution [gResCurrent].v;
  314.     }
  315.     
  316.     // pixel depth
  317.     if (theKey == '\36')
  318.     {
  319.         glInfo.pixelDepth = 32;
  320.     }
  321.     else if (theKey == '\37')
  322.     {
  323.         glInfo.pixelDepth = 16;
  324.     }
  325.     
  326.     // frequency
  327.     if (theKey == '=')
  328.     {
  329.         
  330.         if (++gFreqCurrent == kNumFreqs)
  331.             gFreqCurrent = 0;
  332.         glInfo.freq = gaFreq [gFreqCurrent];
  333.     }
  334.     else if (theKey == '-')
  335.     {
  336.         if (--gFreqCurrent < 0)
  337.             gFreqCurrent = (short) (kNumFreqs - 1);
  338.         glInfo.freq = gaFreq [gFreqCurrent];
  339.     }
  340.     
  341.     // device 
  342.     if (theKey == ']')
  343.         gNumDevice++;
  344.     else if (theKey == '[')
  345.         gNumDevice--;
  346.     
  347.     // device 
  348.     if (theKey == 'w')
  349.         glInfo.fFullscreen = false;
  350.     else if (theKey == 'f')
  351.         glInfo.fFullscreen = true;
  352.     
  353.     glInfo.fSizeMust = false;            // dspContext must be requested height (ignored in window mode)
  354.     glInfo.fDepthMust = false;            // pixel depth must be set (if false then curretn depth will be used if able)
  355.     glInfo.fAcceleratedMust = true;     // must renderer be accelerated?
  356.     glInfo.VRAM = 0 * 1048576;            // minimum VRAM (if not zero this is always required)
  357.     glInfo.textureRAM = 0 * 1048576;    // minimum texture RAM (if not zero this is always required)
  358.  
  359.     // rebuild GL
  360.     if (gpWindowError)
  361.     {
  362.         DisposeWindow ((WindowPtr)gpWindowError);
  363.         gpWindowError = NULL;
  364.     }
  365.  
  366.     DeleteFontGL (gFontList);
  367.     DestroyGL (&aglDraw, &aglContext, &dspContext, &glInfo);
  368.     {
  369.         EventRecord theEvent;
  370.         while (WaitNextEvent(everyEvent, &theEvent, gSleepTime, NULL))    // get all the event to ensure icons are not moved
  371.         {}
  372.     }
  373.     
  374.     if (PreflightGL (glInfo.fFullscreen))
  375.     {
  376.         BuildGL (&aglDraw, &aglContext, &dspContext, &gNumDevice, &glInfo);
  377.         if (!aglDraw || !aglContext)
  378.             DestroyGL (&aglDraw, &aglContext, &dspContext, &glInfo);
  379.     }
  380.         
  381.     if (!aglDraw || !aglContext)
  382.     {
  383.         Rect rectWin = {100, 100, 200, 400};
  384.         gpWindowError = (WindowPtr) NewCWindow (NULL, &rectWin, "\p", 0, kWindowFloatProc, (WindowPtr)-1, 0, 0);
  385.         ShowWindow ((GrafPtr)gpWindowError);
  386.     }
  387.     else
  388.     {    
  389.         GetFNum("\pMonaco", &fNum);                                    // build font
  390.         gFontList = BuildFontGL (aglContext, fNum, normal, 9);
  391.         // set mode string
  392.         sprintf (gstrMode, "%d: %d x %d x %d (%d Hz)", gNumDevice, glInfo.width, glInfo.height, glInfo.pixelDepth, glInfo.freq);
  393.     }
  394.     DoUpdate ();
  395. }
  396.  
  397. // --------------------------------------------------------------------------
  398.  
  399. void DoUpdate (void)
  400. {
  401.     if (aglDraw && aglContext)
  402.         drawGL (aglContext, &glInfo);
  403.     else
  404.     {
  405.         GrafPtr gpSave;
  406.         GetPort (&gpSave);
  407.         SetPort (gpWindowError);
  408.         MoveTo (10, 20);
  409.         DrawString ("\pBuildGL failed.");
  410.         SetPort (gpSave);
  411.     }
  412. }
  413.  
  414. // --------------------------------------------------------------------------
  415.  
  416. void DoEvent (void)
  417. {
  418.     EventRecord theEvent;
  419.     Rect rectGrow;
  420.     SInt32 menuResult;
  421.     WindowRef whichWindow;
  422.     GrafPtr pGrafSave;
  423.     long grow;
  424.     SInt16 whatPart;
  425.     SInt8 theKey;
  426.     SInt8 theCode;
  427.     Boolean fProcessed = false;
  428.     
  429.     if (WaitNextEvent(everyEvent, &theEvent, gSleepTime, NULL))
  430.     {
  431.         if (dspContext)
  432.             DSpProcessEvent (&theEvent, &fProcessed);
  433.         if (!fProcessed)
  434.         {
  435.             switch (theEvent.what)
  436.             {
  437.                 case mouseDown:
  438.                     whatPart = FindWindow(theEvent.where, &whichWindow);
  439.                     switch (whatPart)
  440.                     {
  441.                         case inGoAway:
  442.                             break;
  443.                         case inMenuBar:
  444.                             DrawMenuBar();
  445.                             menuResult = MenuSelect(theEvent.where);
  446.                             if (HiWord(menuResult) != 0)
  447.                                 DoMenu(menuResult);
  448.                             break;
  449.                         case inDrag:
  450.                             DragWindow (whichWindow, theEvent.where, NULL);
  451.                             break;
  452.                         case inGrow:
  453.                             SetRect (&rectGrow, 100, 100, 20000, 20000);
  454.                             grow = GrowWindow (whichWindow, theEvent.where, &rectGrow);
  455.                             if (grow)
  456.                             {
  457.                                 SizeWindow (whichWindow, grow & 0x0000FFFF, grow >> 16, true);
  458.                                 // do content stuff here
  459.                                 SetPort (whichWindow);
  460.                                 InvalRect (&whichWindow->portRect);                // redraw all
  461.                             }
  462.                             break;
  463.                         case inSysWindow:
  464.                             SystemClick(&theEvent, whichWindow);
  465.                             break;
  466.                     }
  467.                     break;
  468.                 case keyDown:
  469.                 case autoKey:
  470.                     theKey = theEvent.message & charCodeMask;
  471.                     theCode = (theEvent.message & keyCodeMask) >> 8;
  472.                     if ((theEvent.modifiers & cmdKey) != 0)
  473.                     {
  474.                         menuResult = MenuKey(theKey);
  475.                         if (HiWord(menuResult) != 0)
  476.                             DoMenu (menuResult);
  477.                     }
  478.                     else
  479.                         DoKey (theKey, theCode);
  480.                     break;
  481.                 case updateEvt:
  482.                     whichWindow = (WindowRef) theEvent.message;
  483.                     GetPort (&pGrafSave);
  484.                     SetPort ((GrafPtr) whichWindow);
  485.                     BeginUpdate(whichWindow);
  486.                     if ((CGrafPtr) whichWindow == aglDraw)
  487.                         DoUpdate();
  488.                     SetPort ((GrafPtr) whichWindow);
  489.                     EndUpdate(whichWindow);
  490.                     SetPort (pGrafSave);
  491.                     break;
  492.                 case diskEvt:
  493.                     break;
  494.                 case osEvt:
  495.                     if (theEvent.message & 0x01000000)        //    Suspend/resume event
  496.                     {
  497.                         if (theEvent.message & 0x00000001)    //    Resume
  498.                         {
  499.                             
  500.                             gSleepTime = kForegroundSleep;
  501.                             if (aglDraw && aglContext)
  502.                             {
  503.                                 HideCursor ();
  504.                                 ShowWindow ((WindowPtr) aglDraw);
  505.                                 SelectWindow ((WindowPtr) aglDraw);
  506.                                 ResumeGL (aglDraw, aglContext);
  507.                             }
  508.                             DoUpdate ();
  509.                             gfFrontProcess = true;
  510.                         }
  511.                         else
  512.                         {
  513.                             gSleepTime = kBackgroundSleep;    //    Suspend
  514.                             if (aglDraw && aglContext)
  515.                             {
  516.                                 PauseGL (aglContext);
  517.                                 HideWindow ((WindowPtr) aglDraw);
  518.                                 ShowCursor ();
  519.                             }
  520.                             gfFrontProcess = false;
  521.                         }
  522.                     }
  523.                     break;
  524.  
  525.                 case kHighLevelEvent:
  526.                     AEProcessAppleEvent(&theEvent);
  527.                     break;
  528.             }
  529.         }
  530.     }
  531.     else
  532.     {
  533.         if (gfFrontProcess)
  534.             DoUpdate ();
  535.     }
  536. }
  537.  
  538. // --------------------------------------------------------------------------
  539.  
  540. void CleanUp (void)
  541. {
  542.     MenuHandle hMenu;
  543.  
  544.     DeleteFontGL (gFontList);
  545.     DestroyGL (&aglDraw, &aglContext, &dspContext, &glInfo);
  546.  
  547.     if (gpWindowError)
  548.     {
  549.         DisposeWindow ((WindowPtr)gpWindowError);
  550.         gpWindowError = NULL;
  551.     }
  552.  
  553.     hMenu = GetMenuHandle (kMenuFile);
  554.     DeleteMenu (kMenuFile);
  555.     DisposeMenu (hMenu);
  556.  
  557.     hMenu = GetMenuHandle (kMenuApple);
  558.     DeleteMenu (kMenuApple);
  559.     DisposeMenu (hMenu);
  560. }
  561.  
  562. // --------------------------------------------------------------------------
  563.  
  564. int main (void)
  565. {
  566.     if (SetUp ())    
  567.         while (!gDone) 
  568.             DoEvent ();
  569.     CleanUp ();
  570.     return 0;
  571. }
  572.  
  573.  
  574. #pragma mark -
  575. //-----------------------------------------------------------------------------------------------------------------------
  576.  
  577. // OpenGL Drawing
  578.  
  579. void drawGL(AGLContext aglContext, pstructGLInfo pglInfo)
  580. {
  581.     static float f, s, c;
  582.     GLboolean fState = GL_FALSE;
  583.  
  584.     f += 0.01;
  585.     s = sin(f);
  586.     c = cos(f);
  587.  
  588.     glClearColor(0.15f, 0.15f, 0.15f, 1.0f);                    // Clear color buffer to dark grey
  589.     glClear(GL_COLOR_BUFFER_BIT);
  590.     
  591.     glBegin(GL_POLYGON);                                        // Draw a smooth shaded polygon
  592.     glColor3d(1.0, 0.0, 0.0);
  593.     glVertex3d(s, c, 0.0);
  594.     glColor3d(0.0, 1.0, 0.0);
  595.     glVertex3d(-c, s, 0.0);
  596.     glColor3d(0.0, 0.0, 1.0);
  597.     glVertex3d(-s, -c, 0.0);
  598.     glColor3d(0.7, 0.7, 0.7);
  599.     glVertex3d(c, -s, 0.0);
  600.     glEnd();
  601.  
  602.     // Draw frame rate (set color and position first)
  603.     glColor3d(1.0, 1.0, 1.0);
  604.     glRasterPos3d (-0.95, (pglInfo->height - 40.0) / (float) pglInfo->height, 0.0); 
  605.     DrawFrameRate (gFontList);
  606.     glRasterPos3d (-0.95, (pglInfo->height - 65.0) / (float) pglInfo->height, 0.0); 
  607.     DrawContextInfo (gFontList);
  608.  
  609.     glRasterPos3d (-0.95, -(pglInfo->height - 45.0) / (float) pglInfo->height, 0.0); 
  610.     DrawCSringGL ((char*) glGetString (GL_VENDOR), gFontList);
  611.     glRasterPos3d (-0.95, -(pglInfo->height - 20.0) / (float) pglInfo->height, 0.0); 
  612.     DrawCSringGL ((char*) glGetString (GL_RENDERER), gFontList);
  613.  
  614.     aglSwapBuffers(aglContext);                                        // send swap command
  615. }
  616.  
  617. //-----------------------------------------------------------------------------------------------------------------------
  618.  
  619. // Draw frame rate in curent color at current raster positon with provided font display list
  620.  
  621. void DrawFrameRate (GLuint fontList)
  622. {    
  623.     static char aChar[256] = "";
  624.     static AbsoluteTime time = {0,0};
  625.     static long frames = 0;
  626.  
  627.     AbsoluteTime currTime = UpTime ();
  628.     float deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
  629.     
  630.     frames++;
  631.  
  632.     if (0 > deltaTime)    // if negative microseconds
  633.         deltaTime /= -1000000.0;
  634.     else                // else milliseconds
  635.         deltaTime /= 1000.0;
  636.     if (0.5 <= deltaTime)    // has update interval passed
  637.     {
  638.         sprintf (aChar, "Swaps/Sec: %0.1f", frames / deltaTime);
  639.         time = currTime;    // reset for next time interval
  640.         frames = 0;
  641.     }
  642.     
  643.     DrawCSringGL (aChar, fontList);
  644. }
  645.  
  646. //-----------------------------------------------------------------------------------------------------------------------
  647.  
  648. // Draw height,width, bit pixel and frequency for context
  649.  
  650. void DrawContextInfo (GLuint fontList)
  651. {    
  652.     DrawCSringGL (gstrMode, fontList);
  653. }
  654.  
  655. #pragma mark -
  656. //-----------------------------------------------------------------------------------------------------------------------
  657.  
  658. void DrawPSringGL (Str255 pstrOut, GLuint fontList)
  659. {
  660.     GLint i;
  661.     for (i = 1; i <= pstrOut[0]; i++)
  662.         glCallList (fontList + pstrOut[i]);
  663. }
  664.  
  665. //-----------------------------------------------------------------------------------------------------------------------
  666.  
  667. void DrawCSringGL (char * cstrOut, GLuint fontList)
  668. {
  669.     GLint i = 0;
  670.     while (cstrOut [i])
  671.         glCallList (fontList + cstrOut[i++]);
  672. }
  673.  
  674. //-----------------------------------------------------------------------------------------------------------------------
  675.  
  676. GLuint BuildFontGL (AGLContext ctx, GLint fontID, Style face, GLint size)
  677. {
  678.     GLuint listBase = glGenLists (256);
  679.     if (aglUseFont (ctx, fontID , face, size, 0, 256, (long) listBase))
  680.     {
  681.         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  682.         return listBase;
  683.     }
  684.     else
  685.     {
  686.         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  687.         glDeleteLists (listBase, 256);
  688.         return 0;
  689.     }
  690. }
  691.  
  692. //-----------------------------------------------------------------------------------------------------------------------
  693.  
  694. void DeleteFontGL (GLuint fontList)
  695. {
  696.     if (fontList)
  697.         glDeleteLists (fontList, 256);
  698. }